<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 13 深色主题切换流程解析 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 13 深色主题切换流程解析">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 13 深色主题切换流程解析">
<meta property="og:url" content="http://longzhiye.top/2023/09/24/2023-09-25/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 13 深色主题切换流程解析">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-09-23T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.909Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Framework">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">

  <meta name="keywords" content=",Android,Framework,Java">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2023/09/24/2023-09-25/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2023/09/24/2023-09-25/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-7.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 13 深色主题切换流程解析</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2023-09-24 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2023/09/24/2023-09-25/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 13 深色主题切换流程解析&url=http://longzhiye.top/2023/09/24/2023-09-25/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 13 深色主题切换流程解析&url=http://longzhiye.top/2023/09/24/2023-09-25/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2023/09/24/2023-09-25/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2023/09/24/2023-09-25/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2023/09/24/2023-09-25/&title=Android 13 深色主题切换流程解析" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 13 深色主题切换流程解析&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2023/09/24/2023-09-25/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2023/09/24/2023-09-25/&text=Android 13 深色主题切换流程解析" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <p>学习笔记：Android小白，这位置网上没资料，通过自己打日志阅读代码走的流程，可能有理解错误的地方。欢迎指正，大家共同进步。</p>
<p>深色主题设置方法：两种设置方法流程是一样的。</p>
<ul>
<li>通过下拉状态栏的快捷按钮深色主题切换；</li>
<li>通过 设置→显示→深色主题开关 切换；</li>
</ul>
<p>本文以下拉状态栏的快捷按钮深色主题切换为例；<br>该快捷按钮类为 <strong>UiModeNightTile.java</strong>，直接看点击事件：</p>
<p><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/qs/tiles/UiModeNightTile.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">handleClick</span><span class="params">(<span class="meta">@Nullable</span> View view)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (getState().state == Tile.STATE_UNAVAILABLE) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="type">boolean</span> <span class="variable">newState</span> <span class="operator">=</span> !mState.value;</span><br><span class="line">    <span class="comment">// 设置主题模式</span></span><br><span class="line">    mUiModeManager.setNightModeActivated(newState);</span><br><span class="line">    <span class="comment">// 更新该开关的状态</span></span><br><span class="line">    refreshState(newState);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>根据上面的我们直接找 UiModeManager.java 的 setNightModeActivated() 方法：<br>UiModeManager#setNightModeActivated()<br><strong>frameworks/base/core/java/android/app/UiModeManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequiresPermission(android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">setNightModeActivated</span><span class="params">(<span class="type">boolean</span> active)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (mService != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// mService 为 UiModeManagerService 的对象</span></span><br><span class="line">            <span class="keyword">return</span> mService.setNightModeActivated(active);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">            <span class="keyword">throw</span> e.rethrowFromSystemServer();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>UiModeManagerService#setNightModeActivated()<br><strong>frameworks/base/services/core/java/com/android/server/UiModeManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">setNightModeActivated</span><span class="params">(<span class="type">boolean</span> active)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> setNightModeActivatedForModeInternal(mNightModeCustomType, active);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">private</span> <span class="type">boolean</span> <span class="title function_">setNightModeActivatedForModeInternal</span><span class="params">(<span class="type">int</span> modeCustomType, <span class="type">boolean</span> active)</span> &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 省略部分代码......            </span></span><br><span class="line">    <span class="keyword">synchronized</span> (mLock) &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">long</span> <span class="variable">ident</span> <span class="operator">=</span> Binder.clearCallingIdentity();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 自动、自定义的主题切换</span></span><br><span class="line">            <span class="keyword">if</span> (mNightMode == MODE_NIGHT_AUTO || mNightMode == MODE_NIGHT_CUSTOM) &#123;</span><br><span class="line">                unregisterScreenOffEventLocked();</span><br><span class="line">                mOverrideNightModeOff = !active;</span><br><span class="line">                mOverrideNightModeOn = active;</span><br><span class="line">                mOverrideNightModeUser = mCurrentUser;</span><br><span class="line">                persistNightModeOverrides(mCurrentUser);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (mNightMode == UiModeManager.MODE_NIGHT_NO</span><br><span class="line">                    &amp;&amp; active) &#123;</span><br><span class="line">                <span class="comment">// 夜间模式</span></span><br><span class="line">                mNightMode = UiModeManager.MODE_NIGHT_YES;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (mNightMode == UiModeManager.MODE_NIGHT_YES</span><br><span class="line">                    &amp;&amp; !active) &#123;</span><br><span class="line">                <span class="comment">// 日间模式</span></span><br><span class="line">                mNightMode = UiModeManager.MODE_NIGHT_NO;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 更新 Configuration</span></span><br><span class="line">            updateConfigurationLocked();</span><br><span class="line">            <span class="comment">// 应用 Configuration</span></span><br><span class="line">            applyConfigurationExternallyLocked();</span><br><span class="line">            <span class="comment">// 为当前用户 Secure.putIntForUser 配置   </span></span><br><span class="line">            persistNightMode(mCurrentUser);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            Binder.restoreCallingIdentity(ident);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>根据上述代码这里主要看下 applyConfigurationExternallyLocked()、persistNightMode() 两个方法；<br>persistNightMode() 方法简单，先看 UiModeManagerService#persistNightMode()<br><strong>frameworks/base/services/core/java/com/android/server/UiModeManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">persistNightMode</span><span class="params">(<span class="type">int</span> user)</span> &#123;</span><br><span class="line">    <span class="comment">// 如果不处于汽车模式，才去设置</span></span><br><span class="line">    <span class="keyword">if</span> (mCarModeEnabled || mCar) <span class="keyword">return</span>;</span><br><span class="line">    <span class="comment">// 这里主要是将  mNightMode 的值 put 到数据库。</span></span><br><span class="line">    Secure.putIntForUser(getContext().getContentResolver(),</span><br><span class="line">            Secure.UI_NIGHT_MODE, mNightMode, user);</span><br><span class="line">    <span class="comment">// 夜间模式自定义类型</span></span><br><span class="line">    Secure.putLongForUser(getContext().getContentResolver(),</span><br><span class="line">            Secure.UI_NIGHT_MODE_CUSTOM_TYPE, mNightModeCustomType, user);</span><br><span class="line">    <span class="comment">// 定义自动夜间模式启动毫秒数</span></span><br><span class="line">    Secure.putLongForUser(getContext().getContentResolver(),</span><br><span class="line">            Secure.DARK_THEME_CUSTOM_START_TIME,</span><br><span class="line">            mCustomAutoNightModeStartMilliseconds.toNanoOfDay() / <span class="number">1000</span>, user);</span><br><span class="line">    <span class="comment">// 自定义自动夜间模式结束毫秒</span></span><br><span class="line">    Secure.putLongForUser(getContext().getContentResolver(),</span><br><span class="line">            Secure.DARK_THEME_CUSTOM_END_TIME,</span><br><span class="line">            mCustomAutoNightModeEndMilliseconds.toNanoOfDay() / <span class="number">1000</span>, user);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>再接着看 UiModeManagerService#applyConfigurationExternallyLocked() 方法：<br><strong>frameworks/base/services/core/java/com/android/server/UiModeManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">applyConfigurationExternallyLocked</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (mSetUiMode != mConfiguration.uiMode) &#123;</span><br><span class="line">        mSetUiMode = mConfiguration.uiMode;</span><br><span class="line">        <span class="comment">// 清除快照缓存，</span></span><br><span class="line">        mWindowManager.clearSnapshotCache();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 这位置很重要，如果把这里注释了 就无法开机了，估计是没用主题了 </span></span><br><span class="line">        ActivityTaskManager.getService().updateConfiguration(mConfiguration);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">            Slog.w(TAG, <span class="string">&quot;Failure communicating with activity manager&quot;</span>, e);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SecurityException e) &#123;</span><br><span class="line">            Slog.e(TAG, <span class="string">&quot;Activity does not have the &quot;</span>, e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述代码将调到 ActivityTaskManagerService#updateConfiguration()<br><strong>frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">updateConfiguration</span><span class="params">(Configuration values)</span> &#123;</span><br><span class="line">    mAmInternal.enforceCallingPermission(CHANGE_CONFIGURATION, <span class="string">&quot;updateConfiguration()&quot;</span>);</span><br><span class="line">    <span class="keyword">synchronized</span> (mGlobalLock) &#123;</span><br><span class="line">        <span class="keyword">if</span> (mWindowManager == <span class="literal">null</span>) &#123;</span><br><span class="line">            Slog.w(TAG, <span class="string">&quot;Skip updateConfiguration because mWindowManager isn&#x27;t set&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (values == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 从窗口管理器获取当前配置</span></span><br><span class="line">            values = mWindowManager.computeNewConfiguration(DEFAULT_DISPLAY);</span><br><span class="line">        &#125;</span><br><span class="line">        mH.sendMessage(PooledLambda.obtainMessage(</span><br><span class="line">                ActivityManagerInternal::updateOomLevelsForDisplay, mAmInternal,</span><br><span class="line">                DEFAULT_DISPLAY));</span><br><span class="line">        <span class="keyword">final</span> <span class="type">long</span> <span class="variable">origId</span> <span class="operator">=</span> Binder.clearCallingIdentity();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (values != <span class="literal">null</span>) &#123;</span><br><span class="line">                Settings.System.clearConfiguration(values);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 重点关注这里。更新配置</span></span><br><span class="line">            updateConfigurationLocked(values, <span class="literal">null</span>, <span class="literal">false</span>, <span class="literal">false</span> <span class="comment">/* persistent */</span>,</span><br><span class="line">                    UserHandle.USER_NULL, <span class="literal">false</span> <span class="comment">/* deferResume */</span>,</span><br><span class="line">                    mTmpUpdateConfigurationResult);</span><br><span class="line">            <span class="keyword">return</span> mTmpUpdateConfigurationResult.changes != <span class="number">0</span>;</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            Binder.restoreCallingIdentity(origId);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">boolean</span> <span class="title function_">updateConfigurationLocked</span><span class="params">(Configuration values, ActivityRecord starting,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> initLocale, <span class="type">boolean</span> persistent, <span class="type">int</span> userId, <span class="type">boolean</span> deferResume,</span></span><br><span class="line"><span class="params">        ActivityTaskManagerService.UpdateConfigurationResult result)</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">changes</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="type">boolean</span> <span class="variable">kept</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line">    deferWindowLayout();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (values != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 更新全局配置</span></span><br><span class="line">            changes = updateGlobalConfigurationLocked(values, initLocale, persistent, userId);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!deferResume) &#123;</span><br><span class="line">            <span class="comment">// 更新后确保配置和可见性</span></span><br><span class="line">            kept = ensureConfigAndVisibilityAfterUpdate(starting, changes);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">        continueWindowLayout();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (result != <span class="literal">null</span>) &#123;</span><br><span class="line">        result.changes = changes;</span><br><span class="line">        result.activityRelaunched = !kept;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> kept;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里直接看更新 updateGlobalConfigurationLocked() 方法：<br><strong>frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">updateGlobalConfigurationLocked</span><span class="params">(<span class="meta">@NonNull</span> Configuration values, <span class="type">boolean</span> initLocale,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> persistent, <span class="type">int</span> userId)</span> &#123;</span><br><span class="line">    <span class="comment">// 省略部分代码......     </span></span><br><span class="line">    SparseArray&lt;WindowProcessController&gt; pidMap = mProcessMap.getPidMap();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> pidMap.size() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> <span class="variable">pid</span> <span class="operator">=</span> pidMap.keyAt(i);</span><br><span class="line">        <span class="keyword">final</span> <span class="type">WindowProcessController</span> <span class="variable">app</span> <span class="operator">=</span> pidMap.get(pid);</span><br><span class="line">        ProtoLog.v(WM_DEBUG_CONFIGURATION, <span class="string">&quot;Update process config of %s to new &quot;</span></span><br><span class="line">                + <span class="string">&quot;config %s&quot;</span>, app.mName, mTempConfig);</span><br><span class="line">        <span class="comment">// 通知每个进程配置更改</span></span><br><span class="line">        <span class="comment">// 这句话注释掉 将无法切换主题模式，不管用。</span></span><br><span class="line">        <span class="comment">// 只需要关注这里就行</span></span><br><span class="line">        app.onConfigurationChanged(mTempConfig);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Message</span> <span class="variable">msg</span> <span class="operator">=</span> PooledLambda.obtainMessage(</span><br><span class="line">            ActivityManagerInternal::broadcastGlobalConfigurationChanged,</span><br><span class="line">            mAmInternal, changes, initLocale);</span><br><span class="line">    mH.sendMessage(msg);</span><br><span class="line">    <span class="comment">// 更新存储的全局配置并通知所有人有关更改。</span></span><br><span class="line">    <span class="comment">// 这里注释了将无法开机，没用任何主题。</span></span><br><span class="line">    mRootWindowContainer.onConfigurationChanged(mTempConfig);</span><br><span class="line">    <span class="keyword">return</span> changes;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述代码跟进去，将会回调 WindowProcessController#onConfigurationChanged()<br><strong>frameworks/base/services/core/java/com/android/server/wm/WindowProcessController.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onConfigurationChanged</span><span class="params">(Configuration newGlobalConfig)</span> &#123;</span><br><span class="line">    <span class="comment">// super 父类 ConfigurationContainer</span></span><br><span class="line">    <span class="built_in">super</span>.onConfigurationChanged(newGlobalConfig);</span><br><span class="line">    updateConfiguration();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>接着看 ConfigurationContainer#onConfigurationChanged()<br><strong>frameworks/base/services/core/java/com/android/server/wm/ConfigurationContainer.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onConfigurationChanged</span><span class="params">(Configuration newParentConfig)</span> &#123;</span><br><span class="line">    <span class="comment">//临时configuration变量，保存更新之前的mResolvedOverrideConfiguration</span></span><br><span class="line">    mResolvedTmpConfig.setTo(mResolvedOverrideConfiguration);</span><br><span class="line">    <span class="comment">//更新mResolvedOverrideConfiguration，可重写此方法增添特殊配置</span></span><br><span class="line">    resolveOverrideConfiguration(newParentConfig);</span><br><span class="line">    mFullConfiguration.setTo(newParentConfig);</span><br><span class="line">    <span class="comment">// mFullConfiguration 是实际更新后 configuration 的最终状态      </span></span><br><span class="line">    mFullConfiguration.windowConfiguration.unsetAlwaysOnTop();</span><br><span class="line">    <span class="comment">//根据差异更新最终状态:mFullConfiguration</span></span><br><span class="line">    mFullConfiguration.updateFrom(mResolvedOverrideConfiguration);</span><br><span class="line">    <span class="comment">//合并此次修改，保存至mMergedOverrideConfiguration</span></span><br><span class="line">    onMergedOverrideConfigurationChanged();</span><br><span class="line">    <span class="keyword">if</span> (!mResolvedTmpConfig.equals(mResolvedOverrideConfiguration)) &#123;</span><br><span class="line">        <span class="comment">//进入此分支，代表特殊配置修改，通知监听者，</span></span><br><span class="line">        <span class="comment">// 当第三方应用设置和默认系统主题不一样的时候，该位置是应用主动请求</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> mChangeListeners.size() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; --i) &#123;</span><br><span class="line">            mChangeListeners.get(i).onRequestedOverrideConfigurationChanged(</span><br><span class="line">                    mResolvedOverrideConfiguration);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    Log.d(<span class="string">&quot;yeruilai&quot;</span>,<span class="string">&quot;yeruilai：&quot;</span>+mChangeListeners.size());</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> mChangeListeners.size() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; --i) &#123;</span><br><span class="line">        Log.d(<span class="string">&quot;yeruilai&quot;</span>,<span class="string">&quot;yeruilai：&quot;</span>+mChangeListeners.get(i));</span><br><span class="line">        <span class="comment">//通知监听者，已经合并父配置修改</span></span><br><span class="line">        mChangeListeners.get(i).onMergedOverrideConfigurationChanged(</span><br><span class="line">                mMergedOverrideConfiguration);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> getChildCount() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; --i) &#123;</span><br><span class="line">        <span class="comment">//传达configuration最终状态到子类</span></span><br><span class="line">        <span class="comment">// 这个方法也很重要，但目前不清楚作用，注释掉将无法开机，</span></span><br><span class="line">        dispatchConfigurationToChild(getChildAt(i), mFullConfiguration);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里看一个堆栈：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  : java.lang.Throwable</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.content.res.AssetManager.rebaseTheme(AssetManager.java:<span class="number">1243</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.content.res.ResourcesImpl$ThemeImpl.rebase(ResourcesImpl.java:<span class="number">1457</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.content.res.Resources$Theme.rebase(Resources.java:<span class="number">1874</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.content.res.Resources.setImpl(Resources.java:<span class="number">372</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ResourcesManager.updateResourcesForActivity(ResourcesManager.java:<span class="number">1224</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ResourcesManager.createBaseTokenResources(ResourcesManager.java:<span class="number">867</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ContextImpl.createActivityContext(ContextImpl.java:<span class="number">3148</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread.createBaseContextForActivity(ActivityThread.java:<span class="number">3799</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:<span class="number">3605</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:<span class="number">3853</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:<span class="number">5832</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:<span class="number">5723</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:<span class="number">71</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:<span class="number">45</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:<span class="number">135</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:<span class="number">95</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:<span class="number">2345</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.os.Handler.dispatchMessage(Handler.java:<span class="number">106</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.os.Looper.loopOnce(Looper.java:<span class="number">208</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.os.Looper.loop(Looper.java:<span class="number">295</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at android.app.ActivityThread.main(ActivityThread.java:<span class="number">7941</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at java.lang.reflect.Method.invoke(Native Method)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:<span class="number">569</span>)</span><br><span class="line">09-<span class="number">24</span> <span class="number">16</span>:<span class="number">35</span>:<span class="number">18.717</span>  <span class="number">3118</span>  <span class="number">3118</span> D yeruilai  :      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:<span class="number">1015</span>)</span><br></pre></td></tr></table></figure>
<p>配置改变后通过 binder 调用 IApplicationThread.scheduleTransaction() 方法。<br>留下问题：配置改变后通过 binder 调用，这中间的流程是怎样的？<br>ClientTransaction#schedule()<br><strong>frameworks/base/core/java/android/app/servertransaction/ClientTransaction.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">schedule</span><span class="params">()</span> <span class="keyword">throws</span> RemoteException &#123;</span><br><span class="line">    mClient.scheduleTransaction(<span class="built_in">this</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后配置改变消息会进入应用进程，经过ActivityThread.H发送消息，执行 mTransactionExecutor.execute(transaction)，进入 TransactionExecutor#executeCallbacks() 方法：<br><strong>frameworks/base/core/java/android/app/servertransaction/TransactionExecutor.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@VisibleForTesting</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">executeCallbacks</span><span class="params">(ClientTransaction transaction)</span> &#123;</span><br><span class="line">   </span><br><span class="line">     <span class="comment">// 省略部分代码......   </span></span><br><span class="line">    <span class="keyword">final</span> <span class="type">int</span> <span class="variable">size</span> <span class="operator">=</span> callbacks.size();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; size; ++i) &#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="type">ClientTransactionItem</span> <span class="variable">item</span> <span class="operator">=</span> callbacks.get(i);</span><br><span class="line">         <span class="comment">// 省略部分代码......   </span></span><br><span class="line">        <span class="comment">// 重点关注这里， </span></span><br><span class="line">        item.execute(mTransactionHandler, token, mPendingActions);</span><br><span class="line">        item.postExecute(mTransactionHandler, token, mPendingActions);</span><br><span class="line">        <span class="comment">// 省略部分代码......     </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>ClientTransactionItem 的实现方法在 ActivityTransactionItem 中，所以进入到 ActivityTransactionItem#execute()<br><strong>frameworks/base/core/java/android/app/servertransaction/ActivityTransactionItem.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title function_">execute</span><span class="params">(ClientTransactionHandler client, IBinder token,</span></span><br><span class="line"><span class="params">        PendingTransactionActions pendingActions)</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">ActivityClientRecord</span> <span class="variable">r</span> <span class="operator">=</span> getActivityClientRecord(client, token);</span><br><span class="line">    <span class="comment">// 抽象方法，在 ActivityRelaunchItem.java 中被实现。</span></span><br><span class="line">    execute(client, r, pendingActions);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>ActivityRelaunchItem#execute()<br><strong>frameworks/base/core/java/android/app/servertransaction/ActivityRelaunchItem.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">execute</span><span class="params">(ClientTransactionHandler client, ActivityClientRecord r,</span></span><br><span class="line"><span class="params">        PendingTransactionActions pendingActions)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (mActivityClientRecord == <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (DEBUG_ORDER) Slog.d(TAG, <span class="string">&quot;Activity relaunch cancelled&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, <span class="string">&quot;activityRestart&quot;</span>);</span><br><span class="line">    <span class="comment">// 重点关注这里</span></span><br><span class="line">    client.handleRelaunchActivity(mActivityClientRecord, pendingActions);</span><br><span class="line">    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>后面就不详细分析了；</p>
<blockquote>
<p>后续流程：ActivityThread#handleRelaunchActivity() → ActivityThread#handleRelaunchActivityInner() → ActivityThread#handleLaunchActivity() → ActivityThread#performLaunchActivity()→ ActivityThread#createBaseContextForActivity() → ContextImpl #createActivityContext() → ResourcesManager#createBaseTokenResources() → ResourcesManager#updateResourcesForActivity() → Resources #setImpl() → Resources#Theme.rebase() → AssetManager#rebaseTheme()<br>这里注意：在 ActivityThread.java 中有 performActivityConfigurationChanged() 和  performLaunchActivity() 两个方法，都可以更新资源主题，我个人认为一个是配置单独某个应用的，一个是配置全局的。</p>
</blockquote>
<p>到此完成应用进程回调。<br>那么系统进程如何传送配置信息到应用进程？<br>这里回到 ActivityTaskManagerService.java。通过ensureConfigAndVisibilityAfterUpdate方法，确保目前启动的activity,重启来加载新的资源<br>ActivityTaskManagerService#ensureConfigAndVisibilityAfterUpdate()<br><strong>frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="title function_">ensureConfigAndVisibilityAfterUpdate</span><span class="params">(ActivityRecord starting, <span class="type">int</span> changes)</span> &#123;</span><br><span class="line">    <span class="type">boolean</span> <span class="variable">kept</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Task</span> <span class="variable">mainRootTask</span> <span class="operator">=</span> mRootWindowContainer.getTopDisplayFocusedRootTask();</span><br><span class="line">    <span class="comment">// mainRootTask is null during startup.</span></span><br><span class="line">    <span class="keyword">if</span> (mainRootTask != <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (changes != <span class="number">0</span> &amp;&amp; starting == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 如果配置改变了，并且调用者还没有在启动一个活动的过程中，</span></span><br><span class="line">            <span class="comment">// 那么找到最上面的活动来检查它的配置是否需要改变</span></span><br><span class="line">            starting = mainRootTask.topRunningActivity();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (starting != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 重点关注这里</span></span><br><span class="line">            kept = starting.ensureActivityConfiguration(changes,</span><br><span class="line">                    <span class="literal">false</span> <span class="comment">/* preserveWindow */</span>);</span><br><span class="line">            mRootWindowContainer.ensureActivitiesVisible(starting, changes,</span><br><span class="line">                    !PRESERVE_WINDOWS);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> kept;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>starting 是 ActivityRecord 对象，所有看 ActivityRecord #ensureActivityConfiguration()<br><strong>frameworks/base/services/core/java/com/android/server/wm/ActivityRecord.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="title function_">ensureActivityConfiguration</span><span class="params">(<span class="type">int</span> globalChanges, <span class="type">boolean</span> preserveWindow,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> ignoreVisibility)</span> &#123;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="keyword">if</span> (changes == <span class="number">0</span> &amp;&amp; !forceNewConfig) &#123;</span><br><span class="line">        ProtoLog.v(WM_DEBUG_CONFIGURATION, <span class="string">&quot;Configuration no differences in %s&quot;</span>,</span><br><span class="line">                <span class="built_in">this</span>);</span><br><span class="line">        <span class="comment">// 不relaunch 时需要去走 scheduleConfigurationChanged让Activity执行onConfiguration的流程</span></span><br><span class="line">        <span class="keyword">if</span> (displayChanged) &#123;</span><br><span class="line">            scheduleActivityMovedToDisplay(newDisplayId, newMergedOverrideConfig);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            scheduleConfigurationChanged(newMergedOverrideConfig);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>displayChanged未改变的前提下，走 scheduleConfigurationChanged()，通知应用进程。<br>ActivityRecord#scheduleConfigurationChanged<br><strong>frameworks/base/services/core/java/com/android/server/wm/ActivityRecord.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">scheduleConfigurationChanged</span><span class="params">(Configuration config)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!attachedToProcess()) &#123;</span><br><span class="line">        ProtoLog.w(WM_DEBUG_CONFIGURATION, <span class="string">&quot;Can&#x27;t report activity configuration &quot;</span></span><br><span class="line">                + <span class="string">&quot;update - client not running, activityRecord=%s&quot;</span>, <span class="built_in">this</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        ProtoLog.v(WM_DEBUG_CONFIGURATION, <span class="string">&quot;Sending new config to %s, &quot;</span></span><br><span class="line">                + <span class="string">&quot;config: %s&quot;</span>, <span class="built_in">this</span>, config);</span><br><span class="line">        mAtmService.getLifecycleManager().scheduleTransaction(app.getThread(), token,</span><br><span class="line">                ActivityConfigurationChangeItem.obtain(config));</span><br><span class="line">    &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">        <span class="comment">// If process died, whatever.</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>至此，应用进程可以根据新配置更新布局等信息。</p>

      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2023/09/24/2023-09-25/">http://longzhiye.top/2023/09/24/2023-09-25/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Framework/" rel="tag">Framework</a><a class="mdui-ripple article_tags-link" href="/tags/Java/" rel="tag">Java</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2023/10/14/2023-10-14/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android Studio错误修复Connect to repo.maven.apache.org:443'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2023/09/03/2023-09-03/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 14(U)适配攻略'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
